import 'dart:async';

import 'package:event_bus/event_bus.dart';

import 'event_bus_param.dart';

class EventBusUtil {
  static EventBus? _eventBus;

  EventBusUtil._internal() {
    _eventBus ??= EventBus();
  }

  static final EventBusUtil _instance = EventBusUtil._internal();

  factory EventBusUtil() => _instance;

  /// 监听事件
  StreamSubscription<EventBusParam>? listen(Function(EventBusParam event) onData) {
    return _eventBus?.on<EventBusParam>().listen(onData);
  }

  /// 发送事件
  void sentEvent(EventBusParam param) {
    _eventBus?.fire(param);
  }

  /// 取消事件
  void cancelEvent(StreamSubscription? streamSubscription) {
    streamSubscription?.cancel();
  }
}

EventBusUtil eventBusUtil = EventBusUtil();
